// 引入
import Vue from 'vue'
import Vuex from 'vuex'
import { _getMusicUrl } from 'network/detail'
import { Song } from 'player/init-songs'


// 使用vuex

Vue.use(Vuex)

// 创建Vuex实例

const store = new Vuex.Store({
    state: {
        theme: 'light',
        userInfo: null,

        /** 歌曲状态 */
        isPlay: false, // 是否正在播放歌曲
        currentIndex: 0, // 歌曲列表正在播放歌曲的index
        _playList: [], // 播放列表
        musicList: [], // 歌曲列表 没有url，通过其id获取url
        
    },
    mutations: {
        setTheme(state,theme) {
            console.log(theme);
            state.theme = theme;
            localStorage.setItem('theme',theme)
        },
        playing(state,isPlay) {
            state.isPlay = isPlay
        },
        setPlayList(state,playList) {

        },
    },
    getters: {
        getTheme(state) {
            if (localStorage.getItem('theme')) {
                state.theme = localStorage.getItem('theme')
            }
            return state.theme
        },
        /** 获取用户信息 */
        getUserInfo(state) {
            if (localStorage.getItem('userInfo')) {
                state.userInfo = JSON.parse(localStorage.getItem('userInfo'))
            }
            return state.userInfo 
        },
        getCurrentIndex(state) {
            if(localStorage.getItem('leaveSongIndex')) {
                state.currentIndex = localStorage.getItem('leaveSongIndex')
            }
            return state.currentIndex
        }
    },
    actions: {
        // getMusicUrl({state,commit}) {
        //     let ids = []
        //     state.musicList.map((item, i) => {
        //         ids.push(item.id)
        //     }) 
        //     _getMusicUrl(ids).then(res => {
        //         if(res.status === 200) {
        //             // let musicUrl = res.data.data[0].url
        //             // let song = new Song(i,item,musicUrl,item.id)
        //             // commit('setPlayList',song)
                    
        //         }
        //     })
        // }
    },
    modules: {
    }
})

export default store // 导出store  在main.js中导入store实例
